package com.atguigu.cloud.Service.impl;

import com.atguigu.cloud.Service.AccountService;
import com.atguigu.cloud.mapper.AccountMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class AccountServiceImpl implements AccountService {

    @Resource
    private AccountMapper accountMapper;

    @Override
    public void decrease(Long userId, Long money) {
        log.info("------->account-service中扣减账户余额开始");
        accountMapper.decrease(userId,money);

//        timeout();
//        int age = 10 / 0;
        log.info("------->account-service中扣减账户余额结束");
    }

//    模拟超时异常, 全局事务回滚
    private static void timeout() {
        try {
            TimeUnit.SECONDS.sleep(65);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
